/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package swym.protocol.server;

import swym.protocol.*;
import swym.server.*;

import java.io.*;
import java.net.*;

/**
 *
 * @author mschaefer
 */
public class RegisterMessageHandler extends MessageHandler
{
    private final static boolean ignoreUnregisteredClient = true;
    
    RegisterActionMessage message;
    
    public RegisterMessageHandler( ActionMessage message ) 
            throws MessageException
    {
        // Tell the constructor not to freak out if the client isn't registered.
        super( message, ignoreUnregisteredClient );
        
        this.message = (RegisterActionMessage) message;
        
        UserManager userManager = UserManager.getInstance();
        
        User user = userManager.getUser( getUserName() );
        
        if ( user == null )
        {
            user = new User( getUserName() );
            
            userManager.registerUser( user );
        }
        
        client = user.getClient( getClientName() );
        
        if ( client == null )
        {
            client = new Client( getClientName() );
            user.addClient( client );
        }
    }
    
    /**
     * 
     * @param socket
     * @throws java.io.IOException
     */
    public void handleMessage( ClientSocket socket ) throws IOException
    {     
        System.out.println("confirming registration");
        writeMessage( socket, new ConfirmActionMessage( userName, 
                                                        clientName, 
                                                        requestID ) );
    }
}
